/*
 * Copyright (C) 2015-2016  Nicola Spanti (RyDroid) <dev@nicola-spanti.info>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */


#include "PlanetWars2dRT-core/specific/model/game_players.h"
#include "PlanetWars2dRT-core/utils/cstring_functions.h"


EXTERN_C_BEGIN


bool
game_add_player(game_t* a_game, const player_t* player_to_add)
{
  for(unsigned char i=0; i < game_get_nb_players(a_game); ++i)
    {
      const player_t* player_i = game_get_const_player_no_check(a_game, i);
      if(cstring_equals(player_to_add->name, player_i->name))
	{
	  return false;
	}
      if(color_rgb_equals(player_to_add->color, player_i->color))
	{
	  return false;
	}
    }
  
  VECTOR_GENERIC_STATIC_PUSH_BACK(&a_game->players, *player_to_add, player_t);
  return true;
}

unsigned char
game_get_player_index_of_player(const game_t* a_game, const player_t* player)
{
  for(unsigned char i=0; i < game_get_nb_players(a_game); ++i)
    {
      if(cstring_equals(player->name,
			game_get_const_player_no_check(a_game, i)->name))
	{
	  return i;
	}
    }
  return NULL_PLAYER_INDEX;
}

void
game_remove_players(game_t* a_game)
{
  for(unsigned char i=0; i < game_get_nb_players(a_game); ++i)
    {
      player_destructor(game_get_player_no_check(a_game, i));
    }
  VECTOR_GENERIC_STATIC_CLEAR(&a_game->players);
}


EXTERN_C_END
